package com.maomi.coder;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * @author zflu
 * Created by lucky on 2019-03-27.
 */
public class LuceneDemo {

    private String[] ids = new String[]{"1","2","3","4"};

    private String[] authors = new String[]{"tim","jim","luckyMao","toni"};

    private String[] bookNames = new String[]{"java","python","go","c++"};

    private String[] prices = new String[]{"100","60","70","90"};


    public IndexWriter getWriter(String path) throws IOException {
        Directory directory = getDirectory(path);
        //可分词 中英文
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        config.setUseCompoundFile(false);
        IndexWriter indexWriter = new IndexWriter(directory,config);

        return indexWriter;
    }

    public Directory getDirectory(String path) throws IOException {
        return FSDirectory.open(Paths.get(path));
    }

    public void addDocument(IndexWriter writer) throws IOException {
        FieldType type = new FieldType();
        type.setStored(true);
        type.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
        type.setTokenized(true);
        for (int i = 0 ; i < 4 ; i ++){
            Document doc = new Document();
            doc.add(new Field("id",ids[i], type));
            doc.add(new StringField("author",authors[i], Field.Store.YES));
            doc.add(new StringField("bookName",bookNames[i], Field.Store.YES));
            doc.add(new Field("price",prices[i],type));

            writer.addDocument(doc);
        }
    }

    public void updateDocument(IndexWriter writer, Term term) throws IOException {

        Document doc = new Document();
        doc.add(new StringField("author","coderMao", Field.Store.YES));
        writer.updateDocument(term,doc);
    }

    public void close(IndexWriter writer) throws IOException {
        writer.close();
    }

    public void delete(IndexWriter writer) throws IOException {
        writer.deleteDocuments(new TermQuery(new Term("author","coderMao")));
    }

    public void read(String path) throws Exception{
        Directory directory = FSDirectory.open(Paths.get(path));
        DirectoryReader open = DirectoryReader.open(directory);
        IndexSearcher is = new IndexSearcher(open);

        QueryParser parser = new QueryParser("id",new StandardAnalyzer());
        Query parse = parser.parse("*:*");
        TopDocs search = is.search(parse, 4);
        System.out.println("评分" + search.getMaxScore());
        for (ScoreDoc scoreDoc:search.scoreDocs) {
            Document doc = is.doc(scoreDoc.doc);
            System.out.println(doc.get("id") + "-" + doc.get("author") + "-" + doc.get("bookName") + "-" + doc.get("price"));
        }
        open.close();
    }


    public static void main(String[] args) throws Exception{
        String path = System.getProperty("java.io.tmpdir") + "index_lucene";
        System.out.println(path);
        LuceneDemo luceneDemo = new LuceneDemo();

        luceneDemo.read(path);

        //获取写索引
//        IndexWriter writer = luceneDemo.getWriter(path);

        //索引操作
//        luceneDemo.addDocument(writer);

        //修改文档
//        Term term = new Term("author:coderMao");
//        luceneDemo.updateDocument(writer,term);
        //删除文档
//        luceneDemo.delete(writer);
//        System.out.println(writer.maxDoc() + "所有数量");
//        System.out.println(writer.numDocs() + "可查数量");
//        writer.commit();
//        System.out.println(writer.maxDoc() + "所有数量");
//        System.out.println(writer.numDocs() + "可查数量");
        //刷新
//        writer.forceMerge(5);
//        luceneDemo.read(path);
//        luceneDemo.close(writer);
    }
}
